#include "head.h"

int tcp_init(char* ip,char* port,int* sersockfd)
{
    int socket_fd=socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(socket_fd,-1,"socket");

    struct  sockaddr_in serAddr;
    serAddr.sin_family=AF_INET;
    serAddr.sin_addr.s_addr=inet_addr(ip);
    serAddr.sin_port=htons(atoi(port));
    //允许地址重用
    int reuse=1;
    int ret=setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(int));
    ERROR_CHECK(ret,-1,"setsockopt");

    ret=bind(socket_fd,(struct sockaddr*)&serAddr,sizeof(serAddr));
    ERROR_CHECK(ret,-1,"bind");

    listen(socket_fd,20);
    *sersockfd=socket_fd;
    // printf("sucess\n");
    return 0;
}